package day9;

/**
 * 题目链接
 * https://www.nowcoder.com/practice/e7e0d226f1e84ba7ab8b28efc6e1aebc?tpId=8&&tqId=11065&rp=1&ru=/activity/oj&qru=/ta/cracking-the-coding-interview/question-ranking
 * 另类加法
 */
public class Main {
    public static int addAB(int A, int B) {
        // write code here
        if(B == 0) {
            return A;
        }
        int sum = 0;
        int carray = 0;
        while(B != 0) {
            sum = A^B;    //亦或运算
            carray = (A&B) << 1;     //与运算判断是否进位(如果为0则代表不需要进位)
            B = carray;
            A = sum;
        }
        return A;
    }

    public static void main(String[] args) {
        addAB(1,2);
    }
}
